﻿using JX.Application;
using JX.Core.Entity;
using JX.Infrastructure.Common;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;

namespace JXWebSite.Areas.Admin.Components
{
	/// <summary>
	/// 公司编辑视图组件
	/// </summary>
	[ViewComponent(Name = "CompanyEdit")]
	public class CompanyEditViewComponent : ViewComponent
	{
		private IDictionaryServiceAppDapper _DictionaryServiceApp;
		/// <summary>
		/// 依赖注入
		/// </summary>
		/// <param name="DictionaryServiceApp"></param>
		public CompanyEditViewComponent(IDictionaryServiceAppDapper DictionaryServiceApp)
		{
			_DictionaryServiceApp = DictionaryServiceApp;
		}
		/// <summary>
		/// 异步调用
		/// </summary>
		/// <param name="viewModel"></param>
		/// <returns></returns>
		public async Task<IViewComponentResult> InvokeAsync(CompanyViewModel viewModel = null)
		{
			//行业地位
			var StatusInFieldList = _DictionaryServiceApp.GetDictionaryFieldValue("Company", "StatusInField");
			StatusInFieldList.Insert(0, new DictionaryValue { DataTextField = "请选择", DataValueField = -1, IsDefault = true });
			var selectListStatusInField = new SelectList(StatusInFieldList, "DataValueField", "DataTextField");
			var selectItemListStatusInField = new List<SelectListItem>();
			selectItemListStatusInField.AddRange(selectListStatusInField);
			ViewBag.StatusInFieldList = selectItemListStatusInField;
			//公司规模
			var CompanySizeList = _DictionaryServiceApp.GetDictionaryFieldValue("Company", "CompanySize");
			CompanySizeList.Insert(0, new DictionaryValue { DataTextField = "请选择", DataValueField = -1, IsDefault = true });
			var selectListCompanySize = new SelectList(CompanySizeList, "DataValueField", "DataTextField");
			var selectItemListCompanySize = new List<SelectListItem>();
			selectItemListCompanySize.AddRange(selectListCompanySize);
			ViewBag.CompanySizeList = selectItemListCompanySize;
			//经营状态
			var ManagementFormsList = _DictionaryServiceApp.GetDictionaryFieldValue("Company", "ManagementForms");
			ManagementFormsList.Insert(0, new DictionaryValue { DataTextField = "请选择", DataValueField = -1, IsDefault = true });
			var selectListManagementForms = new SelectList(ManagementFormsList, "DataValueField", "DataTextField");
			var selectItemListManagementForms = new List<SelectListItem>();
			selectItemListManagementForms.AddRange(selectListManagementForms);
			ViewBag.ManagementFormsList = selectItemListManagementForms;

			viewModel = await GetViewModelAsync(viewModel);
			return View(viewModel);
		}

		private Task<CompanyViewModel> GetViewModelAsync(CompanyViewModel viewModel)
		{
			if (viewModel == null)
			{
				viewModel = new CompanyViewModel();
			}
			return Task.FromResult(viewModel);
		}
	}
}
